package leetcode_2501_2600;

import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TopLevelAttribute;

import java.util.*;

public class LeeCode_2512 {
    public static void main(String[] args) {
        System.out.println(topStudents(new String[]{"smart", "brilliant", "studious"}, new String[]{"not"}, new String[]{"this student is not studious", "the student is smart"},
                new int[]{1, 2}, 2));
    }
    private static List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
        Set<String> nag = new HashSet<>(Arrays.asList(negative_feedback));
        Set<String> pos = new HashSet<>(Arrays.asList(positive_feedback));
        int n = report.length;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int score = 0;
            String[] ss = report[i].split(" ");
            for (String s : ss) {
                if (nag.contains(s)){
                    score--;
                }
                if (pos.contains(s)){
                    score += 3;
                }
            }
            int id = student_id[i];
            map.put(id, map.getOrDefault(id, 0) + score);
        }
        TreeMap<Integer, List<Integer>> tree = new TreeMap<>((o1, o2) -> o2 - o1);
        for (Integer key : map.keySet()) {
            int score = map.get(key);
            if (tree.containsKey(score)){
                tree.get(score).add(key);
            }else {
                List<Integer> list = new ArrayList<>();
                list.add(key);
                tree.put(score, list);
            }
        }
        int c = 0;
        List<Integer> ans = new ArrayList<>();
        while (c < k){
            List<Integer> list = tree.pollFirstEntry().getValue();
            list.sort(Comparator.comparingInt(o -> o));
            for (Integer res : list) {
                ans.add(res);
                c++;
                if (c == k)
                    break;
            }
        }
        return ans;
    }
}
